package org.github.core.logger.p6spy;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import java.util.Map;
import lombok.val;
import static cn.hutool.core.text.CharSequenceUtil.isBlank;
import static cn.hutool.core.util.StrUtil.format;

public class P6SpyLogger implements MessageFormattingStrategy {
  @Override
  public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
    if (isBlank(sql)) return "";
    val txt    = sql.replaceAll("\\s+", " ");
    val params = Map.<String,Object>of("elapsed", elapsed, "url", url, "txt", txt);
    return format("cost {elapsed} ms || URL：{url} || SQL：{txt}", params);
  }
}
